package com.yj.consumer.configuration;

import com.yj.consumer.pojo.Pet;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfiguration implements WebMvcConfigurer {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {

        return new RestTemplate();
    }

    @Bean
    public Converter<String, Pet> petConverter() {
        return new Converter<String, Pet>() {
            @Override
            public Pet convert(String s) {
                if (!StringUtils.isEmpty(s)) {
                    Pet pet = new Pet();
                    String[] strings = s.split(",");
                    pet.setName(strings[0]);
                    pet.setPrice(Double.parseDouble(strings[1]));
                     return pet;
                }
                return null;
            }
        };
    }
}

